import { login, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const useUserStore = defineStore(
  'user',
  {
    state: () => ({
      token: getToken(),
      permissions: [],
      userInfo: {},
      roles: []
    }),
    actions: {
      // 登录
      login(userInfo) {
        const username = userInfo.username.trim()
        const password = userInfo.password
        const uuid = userInfo.uuid;
        const captchaKey = userInfo.captchaKey;
        return new Promise((resolve, reject) => {
          // setToken(123456);
          // this.token=123456;
          login({ username, password , uuid , captchaKey }).then(res => {
            if(res.code == 200){
              setToken(res.data)
              this.token = res.data;
              resolve();
            }else{
              reject(res.msg);
            }
          });
        })
      },
      // 获取用户信息
      getInfo() {
        return new Promise((resolve, reject) => {
          // this.permissions=['home:system:select','system:role:edit','system:role:remove'];
          getInfo().then(res => {
            if(res.code == 200){
              this.userInfo = res.data.user;
              this.roles = res.data.roles;
              this.permissions = res.data.permissions;
              resolve();
            }else{
              reject(res.msg);
            }
          });
        })
      },
      // 退出系统
      logOut() {
        return new Promise((resolve, reject) => {
          this.token = '';
          removeToken()
          resolve();
        })
      }
    },
    mutations: {

    }
  })

export default useUserStore
